<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/6/9
 */

namespace app\educational\model;


use think\Db;
use think\Model;

class StudentScoreModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取学生成绩列表
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getScore($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [
            'ss.delete_time' => ['=', 0],
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['ss.id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }

        $max = $this->alias('ss')
            ->join('student st','ss.student_id=st.id')
            ->join('class cl','ss.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('award_student as','as.student_id=ss.student_id and as.class_id=ss.class_id','left')
            ->join('award_card ac','ac.id=as.award_id','left')
            ->where($where)
            ->max('ss.total_score');

        $min = $this->alias('ss')
            ->join('student st','ss.student_id=st.id')
            ->join('class cl','ss.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('award_student as','as.student_id=ss.student_id and as.class_id=ss.class_id','left')
            ->join('award_card ac','ac.id=as.award_id','left')
            ->where($where)
            ->min('ss.total_score');

        $average = $this->alias('ss')
            ->join('student st','ss.student_id=st.id')
            ->join('class cl','ss.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('award_student as','as.student_id=ss.student_id and as.class_id=ss.class_id','left')
            ->join('award_card ac','ac.id=as.award_id','left')
            ->where($where)
            ->avg('ss.total_score');

        $result = $this->alias('ss')
            ->field('ss.*,st.name as student_name,st.code,st.grade,sc.name as school_name,cl.name as class_name,cr.course_name,ac.award_money,ac.valid_date')
            ->join('student st','ss.student_id=st.id')
            ->join('class cl','ss.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('course cr','cl.course_id=cr.id')
            ->join('award_student as','as.student_id=ss.student_id and as.class_id=ss.class_id','left')
            ->join('award_card ac','ac.id=as.award_id','left')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        $data = [
            'max' => $max,
            'min' => $min,
            'average' => $average,
            'data' => $result,
        ];

        return $data;
    }

    /**
     * 编辑学生成绩
     * @param array $data
     * @return bool
     */
    public function editScore($data=[])
    {
        $result = true;
        $id = intval($data['id']);

        $oldScore = $this->where('id', $id)->find();
        if(!isset($oldScore->id)){
            $result = false;
        }else{
            self::startTrans();
            try{
                $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

                self::commit();
            }catch(\Exception $e){
                self::rollback();
                $result = false;
            }
        }

        return $result;
    }

    /**
     * 通过字段获取学生成绩
     * @param array $filter
     * @return array|false|\PDOStatement|string|Model
     */
    public function getScoreByField($filter=[])
    {
        //默认条件
        $where = [
            'delete_time' => ['eq', 0] //未删除
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        $obj = $this->where($where)->find();

        return $obj;
    }

    /**
     * @param $id
     * @param $cur_time
     * @return bool
     */
    public function deleteScore($id, $cur_time)
    {
        $result = true;
        if(empty($id)){
            $result = false;
        }else{
            if(empty($cur_time)){
                //当前时间
                $cur_time = time();
            }
            if(is_array($id)){
                //批量删除
                self::startTrans();
                try{
                    $this->where(['id' => ['in', $id]])->update(['delete_time' => $cur_time]);

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }else{
                //单一删除
                self::startTrans();
                try{
                    $this->where(['id' => $id])->update(['delete_time' => $cur_time]);

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }
        }

        return $result;
    }
}